<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="上传入仓文件" v-model="visible" align-center width="500px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="入仓文件">
        <ImageUpload v-model="form.receipt_file" drag list-type="text" :isShowTip="false" class="w-full v-image">
        </ImageUpload>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import ImageUpload from '@/components/ImageUpload'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({ receipt_file: [] })

// 打开弹窗
const open = async (wr_ids) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { wr_ids, receipt_file: [], remark: '' }
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async () => {
  if (!form.value.receipt_file?.length) {
    ElMessage.error('上传文件不能为空')
    return
  }
  loadings.create = true
  api.uploadReceipt(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>

<style lang="scss">
.v-image {
  .el-upload-dragger {
    padding: 5px;
  }
}
</style>